﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DungDungShit
{
    abstract class NonTile : Sprite
    {

        Texture2D leftTexture, rightTexture, upTexture, downTexture;
        public abstract int Speed { get; }
        private Direction selfDirection = Direction.None;
        public Direction SelfDirection
        {
            set { selfDirection = value; }
            get { return selfDirection; }
        }

        public NonTile(Texture2D defaultTexture, Texture2D leftTexture, Texture2D rightTexture, Texture2D upTexture, Texture2D downTexture,
            IntVector2 position, IntVector2 frameSize, IntVector2 sheetSize)
            : base(defaultTexture, position, frameSize, sheetSize)
        {
            this.leftTexture = leftTexture;
            this.rightTexture = rightTexture;
            this.upTexture = upTexture;
            this.downTexture = downTexture;
        }

        public bool OnExactGrid { get { return SubPosition == IntVector2.Zero; } }

        Direction past = Direction.Down;
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 origin)
        {
            Texture2D texture = SelfDirection.Y == -1 ? upTexture :
                SelfDirection.Y == 1 ? downTexture :
                SelfDirection.X == -1 ? leftTexture :
                SelfDirection.X == 1 ? rightTexture : defaultTexture;
            if (texture.Equals(defaultTexture))
            {
                int ang = 0;
                if (past == Direction.Left) ang = 1;
                else if (past == Direction.Right) ang = 2;
                else if (past == Direction.Up) ang = 3;

                spriteBatch.Draw(texture,
                    new Vector2(Position.X * TileSize.X / 2 + SubPosition.X, Position.Y * TileSize.Y / 2 + SubPosition.Y),
                    new Rectangle(ang * frameSize.X,
                        0 * frameSize.Y,
                        frameSize.X, frameSize.Y),
                    Color.White, 0, origin,
                    1f, SpriteEffects.None, layerDepth);
            }
            else
            {
                if (SelfDirection.Y == -1) past = Direction.Up;
                else if (SelfDirection.Y == 1) past = Direction.Down;
                else if (SelfDirection.X == -1) past = Direction.Left;
                else if (SelfDirection.X == 1) past = Direction.Right;

                DrawTexture(texture, spriteBatch, origin);
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Position == null) return;

            IntVector2 prevSubPosition = SubPosition;
            SubPosition += Velocity;
            if (IntVector2.DotProduct(prevSubPosition, SubPosition) < 0)
                SubPosition = IntVector2.Zero;
            if (SubPosition.X >= TileSize.X / 2)
            {
                SubPosition.X -= TileSize.X / 2;
                Position.X++;
            }
            else if (SubPosition.X <= -TileSize.X / 2)
            {
                SubPosition.X += TileSize.X / 2;
                Position.X--;
            }

            if (SubPosition.Y >= TileSize.Y / 2)
            {
                SubPosition.Y -= TileSize.Y / 2;
                Position.Y++;
            }
            else if (SubPosition.Y <= -TileSize.Y / 2)
            {
                SubPosition.Y += TileSize.Y / 2;
                Position.Y--;
            }
            
            base.Update(gameTime);
        }

    }
}
